<?php
/**
 * Created by b17
 * 2/27/12 8:32 PM
 */

namespace Managers\Builder;

class AdBuilder
{
    private $_dateFormat = 'm/d/Y';

    /**
     * @var \Doctrine\ORM\QueryBuilder
     */
    protected $qb;

    public function __construct()
    {
        $this->reset();
    }

    public function reset()
    {
        $manager = new \Managers\Ad();
        $this->qb = $manager->createQueryBuilder();
    }

    public function setSubCategory($subCatId)
    {
        $this->qb->andWhere('sc.id = :subCat')->setParameter('subCat', $subCatId);
        return $this;
    }

    public function setLimit($limit, $offset = 0)
    {
        $this->qb->setMaxResults($limit)
            ->setFirstResult($offset);
        return $this;
    }

    public function setUser($userId)
    {
        $this->qb->andWhere('u.id = :user')->setParameter('user', $userId);
        return $this;
    }

    public function setId($id)
    {
        $this->qb->andWhere('a.id = :id')->setParameter('id', $id);
        return $this;
    }

    public function getTotal()
    {
        $qb = clone $this->qb;
        $qb->select('count(a.id) as total');
        $res = $qb->getQuery()->getArrayResult();
        return $res[0]['total'];
    }

    public function getList()
    {
        $res = $this->qb->getQuery()->getArrayResult();
        return $res;
    }

    public function getOne()
    {
        $res = $this->qb->getQuery()->getArrayResult();
        return count($res) ? $res[0] : false;
    }

    public function getOneEntity()
    {
        $res = $this->qb->getQuery()->getResult();
        return count($res) ? $res[0] : false;
    }

    public function addOrderById($order = 'DESC')
    {
        $this->qb->addOrderBy('a.id', $order);
        return $this;
    }

    public function getResult()
    {
        $res = $this->qb->getQuery()->getResult();
        return $res;
    }

    public function addOrderByPromotedDate($order = 'DESC')
    {
        $this->qb->addOrderBy('a.promotedDate', $order);
        return $this;
    }

    public function setOnlyPromoted()
    {
        $now = new \DateTime();
        $this->qb->andWhere('a.promotedDate > :now')->setParameter('now', $now);
        return $this;
    }

    public function setSubCat($subCatId)
    {
        if ($subCatId) {
            $this->qb->andWhere('sc.id = :subcat')->setParameter('subcat', $subCatId);
        }
        return $this;
    }

    public function setCategory($categoryId)
    {
        if ($categoryId) {
            $this->qb->andWhere('c.id = :category')->setParameter('category', $categoryId);
        }
        return $this;
    }

    public function setDateRange(\DateTime $start, \DateTime $end)
    {
        $this->qb->andWhere('a.date >= :start')->setParameter('start', $start)
            ->andWhere('a.date < :end')->setParameter('end', $end);
        return $this;
    }

    public function setDateStart($start)
    {
        if (is_string($start)) {
            $start = $this->_createDateTime($start);
        }
        if ($start instanceof \DateTime) {
            $this->qb->andWhere('a.date > :start')->setParameter('start', $start);
        }
        return $this;
    }

    public function setDateEnd($end)
    {
        if (is_string($end)) {
            $end = $this->_createDateTime($end);
        }
        if ($end instanceof \DateTime) {
            $this->qb->andWhere('a.date < :end')->setParameter('end', $end);
        }
        return $this;
    }

    private function _createDateTime($timeString)
    {
        return \DateTime::createFromFormat($this->_dateFormat, $timeString);
    }

    public function setDateFormat($dateFormat)
    {
        $this->_dateFormat = $dateFormat;
    }
}
